//login.js

// wx.showToast({
//   title: '温馨提示',
//   icon: 'error',
//   duration: 2000
// })
// wx.showModal({
//   title: '温馨提示',
//   content: '请输入用户名！',
//   showCancel: false,
//   success(res) {
//     // if (res.confirm) {
//     //   console.log('用户点击确定')
//     // } else if (res.cancel) {
//     //   console.log('用户点击取消')
//     // }
//   }
// })


//获取应用实例
const app = getApp()
Page({
  data: {
    loginId: '',
    loginPW: '',
  },
  doLogin() {
    wx.setStorageSync('token', '')//TODO
    if (this.data.loginId == '') {

      wx.showModal({
        title: '温馨提示',
        content: '请输入用户名！',
        showCancel: false,
        success(res) {

        }
      })
    }
    if (this.data.loginPW == '') {
      wx.showModal({
        title: '温馨提示',
        content: '请输入密码！',
        showCancel: false,
        success(res) {
        }
      })
    }
    let formData = {
      username: this.data.loginId,
      password: this.data.loginPW,
      appType: 'MiniProgram'
    }
    wx.showLoading({
      title: '加载中',
    })

    console.log(formData)
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: app.globalData.url + 'login',
      data: formData,
      method: 'POST',
      success(res) {
        wx.hideLoading({
          success: (res) => { },
        })
        console.log('--------------------------success------------------------------');
        let data = res.data;
        if (data.code == 0) {
          app.globalData.token = res.data.data;
          wx.setStorageSync('token', app.globalData.token)//TODO
          app.get(
            "sysUser/getLoginUser",
            {},
            (res2) => {
              console.log(res2);

              wx.setStorage({
                key: "gzny_loginid",
                data: formData.username
              });
              app.globalData.companyName = res2.data.companyName;
              wx.reLaunch({
                url: '../home/home',
              })
            },
            (err) => {

              wx.showModal({
                title: '温馨提示',
                content: err.msg,
                showCancel: false
              })
            }
          );



        } else {
          wx.hideLoading({
            success: (res) => { },
          })
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      fail(err) {
        wx.hideLoading({
          success: (res) => { },
        })
        console.log('--------------------------error------------------------------');
        wx.showModal({
          title: '温馨提示',
          content: err.msg,
          showCancel: false
        })
      }
    })





  },
  doRegister() {
    wx.navigateGo({
      url: '../register/register',
    })
  },

  onShow() {
    let _this = this;
    wx.getStorage({
      key: 'gzny_loginid',
      success(res) {
        _this.setData({
          loginId: res.data
        })
      }
    })
  },

})